﻿using System.Diagnostics;
using System.IO;
using System.Text;

namespace common
{
    public class bianyi_cc : bianyi
    {
        public override void work(string sPath)
        {
            string soft = strGreen + "\\c#\\c#.bat";
            //string sPath = TreeViewHelper.GetPath(treeViewDirect.SelectedNode);

            string dir = Path.GetDirectoryName(sPath);
            string driver = Directory.GetDirectoryRoot(sPath);

            driver = driver.Replace("\\", "");

            string file = Path.GetFileName(sPath);
            string fileWithoutExtension = Path.GetFileNameWithoutExtension(sPath);

            Encoding encode = Encoding.Default;

            StreamReader sr = new StreamReader(soft);
            string temp = sr.ReadToEnd();

            temp = temp.Replace("$dir", dir);
            temp = temp.Replace("$driver", driver);
            temp = temp.Replace("$fileWithoutExtension", fileWithoutExtension);

            FileInfo aFile = new FileInfo(Path.Combine(dir, "编译.zr"));
            if (aFile.Exists)
            {
                StreamReader srOne = aFile.OpenText();
                string strFile = srOne.ReadToEnd();
                temp = temp.Replace("csc \"$file\"", strFile);
            }
            else
            {
                temp = temp.Replace("$file", file);
            }

            StreamWriter sw = new StreamWriter(strGreen + "\\c#\\temp.bat", false, encode);
            sw.Write(temp);
            sw.Flush();
            sw.Close();

            Process.Start(strGreen + "\\c#\\temp.bat");
        }
    }
}